package sky.dao.Impl;

import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import sky.dao.CardDAO;
import sky.domain.Card;

/**
 * Created with IntelliJ IDEA.
 * User: Gauss
 * Date: 09.05.13
 * Time: 17:48
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class CardDAOImpl implements CardDAO {

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public void addCard(Card card) {
        sessionFactory.getCurrentSession().save(card);
    }

    @Override
    public void removeCard(Integer id) {
        Card card = (Card) sessionFactory.getCurrentSession().load(Card.class, id);
        if (null != card) {
            sessionFactory.getCurrentSession().delete(card);
        }
    }

    @Override
    public Card getCardByCardId(Integer cardId) {
        Query query = sessionFactory.getCurrentSession().createQuery("from Card as card where card.cardId = ?");
        query.setParameter(0, cardId, Hibernate.INTEGER);
        return (Card) query.uniqueResult();
    }
}
